package top.ivansong92.service.office.tools;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import top.ivansong92.service.office.tools.excel.CallBack;

import java.io.*;
import java.util.List;

public class ExcelUnits {

    public static void readExcel(InputStream stream, String fileName) {
        Workbook workbook = null;
        try {
            workbook = fileName.contains(".xlsx") ? new XSSFWorkbook(stream) : new HSSFWorkbook(stream);
            Sheet sheet = workbook.getSheetAt(0);
            int rowCount = sheet.getLastRowNum();
            if (rowCount <= 0) {
                return;
            }

            for (int i = 1; i <= rowCount; i++) {
                Row row = sheet.getRow(i);
                System.out.println(getCellValue(row, 0));
            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public static void readExcel(InputStream stream, String fileName, CallBack<Row> callBack) {
        Workbook workbook = null;
        try {
            workbook = fileName.contains(".xlsx") ? new XSSFWorkbook(stream) : new HSSFWorkbook(stream);
            Sheet sheet = workbook.getSheetAt(0);
            int rowCount = sheet.getLastRowNum();
            if (rowCount <= 0) {
                return;
            }

            for (int i = 1; i <= rowCount; i++) {
                Row row = sheet.getRow(i);
                callBack.callBack(row);
            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }


    public static String getCellValue(Row row, int index) {
        if (row == null || index < 0) {
            return null;
        }
        Cell cell = row.getCell(index);
        if (cell == null) {
            return null;
        }

        String res = null;
        switch (cell.getCellTypeEnum()) {
            case BLANK:
                res = "";
                break;
            case STRING:
                res = cell.getStringCellValue();
                break;
            case BOOLEAN:
                res = cell.getBooleanCellValue() + "";
                break;
            case NUMERIC:
                res = cell.getNumericCellValue() + "";
                break;
        }

        return res;
    }

    public static void readCsv(String fileName) {
        try (BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"))) {
            String record;
            while ((record = file.readLine()) != null) {
                System.out.println(record);
            }
        } catch (Exception e) {

        }
    }

    public static void readCsv(String fileName, CallBack<String> callBack) {
        try (BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"))) {
            String record;
            while ((record = file.readLine()) != null) {
                callBack.callBack(record);
            }
        } catch (Exception e) {

        }
    }



    public static void writeCSVFile(List<String> head, List<List<String>> dataList,
                                     String outPutPath, String filename) {

        File csvFile = new File(outPutPath + File.separator + filename + ".csv");

        try(BufferedWriter csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024)) {

            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();

            // 写入文件头部
            writeRow(head, csvWtriter);

            // 写入文件内容
            for (List<String> row : dataList) {
                writeRow(row, csvWtriter);
            }
            csvWtriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void writeRow(List<String> row, BufferedWriter csvWrietr) throws IOException {
        // 写入文件头部
        String line = String.join(",", row);
        csvWrietr.write(line);
        csvWrietr.newLine();
    }
}
